import QuadTree, { QPoint, QRect } from "./QuadTree";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Graphics)
    graphics: cc.Graphics = null;

    @property(cc.Node)
    xx: cc.Node = null;

    @property(cc.Label)
    checkInfo: cc.Label = null;

    private width:number = 1920;
    private height:number = 1080;

    private _quadTree:QuadTree<cc.Node> = new QuadTree<cc.Node>(0,0,this.width,this.height,50);
    private items:cc.Node[] = [];


    onLoad () {
        this.node.on(cc.Node.EventType.MOUSE_MOVE,  (event:cc.Event.EventMouse) => {
            let pos = event.getLocation();
            this.xx.x = pos.x - this.width/2;
            this.xx.y = pos.y - this.height/2;
        }, this);
    }

    start () {
        for(let i=0; i<1000;i++){
            let node = cc.instantiate(this.xx);
            node.x = (Math.random() - 0.5) * this.width;
            node.y = (Math.random() - 0.5) * this.height;
            let spriteCom:cc.Sprite = this.xx.getComponent(cc.Sprite);
            node.setContentSize( Math.random() * 30 + 5,Math.random() * 30 + 5);
            this.node.addChild(node);
            let rect = node.getBoundingBox();
            let tmpRect = new QRect(new QPoint(this.width/2 + rect.xMin,this.height/2 + rect.yMin),new QPoint(rect.size.width,rect.size.height));
            this._quadTree.update(node,tmpRect);
            this.items.push(node);
            node["vx"] = Math.random() * 6 - 3;
            node["vy"] = Math.random() * 6 - 3;
        }
        this.xx.color = cc.Color.RED;
        this.xx.opacity = 130;
        this.xx.zIndex = 1;
        this.checkInfo.node.zIndex = 1;

        let gridSize = this._quadTree.depthSize[this._quadTree.maxDepth-1];
        for(let i=0 , n = Math.pow(2,this._quadTree.maxDepth); i<n; i++){
            this.graphics.moveTo(-this.width/2,this.height/2 - i * gridSize.h);
            this.graphics.lineTo(this.width/2,this.height/2 - i * gridSize.h);
            this.graphics.close();
            this.graphics.stroke();
        }

        for(let i=0 , n = Math.pow(2,this._quadTree.maxDepth); i<n; i++){
            this.graphics.moveTo(-this.width/2 + i * gridSize.w,-this.height/2);
            this.graphics.lineTo(-this.width/2 + i * gridSize.w,this.height/2);
            this.graphics.close();
            this.graphics.stroke();
        }
    }

    update (dt) {
        
        for(let i=0; i<this.items.length;i++){
            let node = this.items[i];
            node.x += node["vx"];
            node.y += node["vy"];
            if(node.x > this.width/2 || node.x < -this.width/2){
                node["vx"] *= -1;
            }
            if(node.y > this.height/2 || node.y < -this.height/2){
                node["vy"] *= -1;
            }
            let rect = node.getBoundingBox();
            let tmpRect = new QRect(new QPoint(this.width/2 + rect.xMin,this.height/2 + rect.yMin),new QPoint(rect.size.width,rect.size.height));
            this._quadTree.update(node,tmpRect);
            node.color = cc.Color.WHITE;
        }


        console.time("四叉树碰撞耗时:");
        for(let i=0; i<this.items.length;i++){
            let node = this.items[i];
            let rect = node.getBoundingBox();
            let tmpRect = new QRect(new QPoint(this.width/2 + rect.xMin,this.height/2 + rect.yMin),new QPoint(rect.size.width,rect.size.height));
            this._quadTree.update(node,tmpRect);
            node.color = cc.Color.WHITE;
            let result:cc.Node[] = [];
            this._quadTree.quary(tmpRect,result,[]);
            for(let i=0; i<result.length;i++){
                if(result[i] != node){
                    node.color = result[i].color = cc.Color.ORANGE;
                }
            }
        }
        console.timeEnd("四叉树碰撞耗时:");



        // console.time("全遍历碰撞耗时:");
        // for(let i=0; i<this.items.length;i++){
        //     let node = this.items[i];
        //     let rect = node.getBoundingBox();
        //     let tmpRect = new QRect(new QPoint(this.width/2 + rect.xMin,this.height/2 + rect.yMin),new QPoint(rect.size.width,rect.size.height));
        //     node.color = cc.Color.WHITE;
        //     for(let j=0; j<this.items.length;j++){
        //         let node2 = this.items[j];
        //         if(node2 == node) continue;
        //         let rect2 = node2.getBoundingBox();
        //         let tmpRect2 = new QRect(new QPoint(this.width/2 + rect2.xMin,this.height/2 + rect2.yMin),new QPoint(rect2.size.width,rect2.size.height));
        //         if(QRect.intersects(tmpRect,tmpRect2)){
        //             node.color = cc.Color.ORANGE;
        //         }
        //     }
        // }
        // console.timeEnd("全遍历碰撞耗时:");
        


        let result:cc.Node[] = [];
        let visResult:cc.Node[] = [];
        let rect = this.xx.getBoundingBox();
        let tmpRect = new QRect(new QPoint(this.width/2 + rect.xMin,this.height/2 + rect.yMin),new QPoint(rect.size.width,rect.size.height));   
        this._quadTree.quary(tmpRect,result,visResult);
        for(let i=0; i<visResult.length;i++){
            visResult[i].color = cc.Color.BLUE;
        }

        for(let i=0; i<result.length;i++){
            result[i].color = cc.Color.YELLOW;
        }

        this.checkInfo.string = "检测百分比:" + Math.floor((visResult.length / 1000) * 10000)/100 + "%";
    }
}
